#include <mysql/mysql.h>
#include <stdlib.h>
#include <stdio.h>
MYSQL mysql;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;


int main(){
    char host[32]="localhost";
    char user[32]="root";
    char passwd[32]="TPo**db";
    char dbname[32]="test";
    int res;
    if (mysql_init(&mysql)==NULL)
    {
        printf("inital mysql handle error\n");
        return 1;
    }
    if (mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0))
    {
        printf("Connection success\n");
        res = mysql_query(&mysql, "SELECT * FROM test");
        if (res) {
            printf("SELECT error: %s\n", mysql_error(&mysql));
        } 
        else
        {
            res_ptr = mysql_store_result(&mysql);
            if (res_ptr) 
            {
                printf("Retrieved %ld rows\n",(unsigned long)mysql_num_rows(res_ptr));
                while ((sqlrow = mysql_fetch_row(res_ptr))) 
                {
                    printf("Fetched data...\n");
                    printf("sqlrow=%d\n",atoi(sqlrow[0]));
                }
                if (mysql_errno(&mysql)) 
                {
                    fprintf(stderr, "Retrive error: s\n",mysql_error(&mysql)); 
                }
            }
            mysql_free_result(res_ptr);
        }
        mysql_close(&mysql);
    } 
    else 
    {
        fprintf(stderr, "Connection failed\n");
        if (mysql_errno(&mysql)) {
        fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&mysql),mysql_error(&mysql));
        }
    }

    return EXIT_SUCCESS;

}
